package com.uniontech.template.dao;

import com.uniontech.template.entity.FileEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SysFileEntityDao {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    public boolean add(FileEntity fileEntity) {
        String sql = "INSERT INTO file (file_path,file_url,file_name,file_len,ext_name,del_flg) " +
                "VALUES (:filePath,:fileUrl,:fileName,:fileLen,:extName,:delFlg)";
        SqlParameterSource ps = new BeanPropertySqlParameterSource(fileEntity);
        int count = jdbcTemplate.update(sql, ps);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public FileEntity getFileEntityByPath(String filePath) {
        String sql = "select sfu.* from file sfu where sfu.del_flg=:delFlg";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("delFlg", 1);
        sql+=" and sfu.file_path=:filePath";
        map.put("filePath", filePath);
        SqlParameterSource ps = new MapSqlParameterSource(map);
        List<FileEntity> list = jdbcTemplate.query(sql, ps, new BeanPropertyRowMapper<FileEntity>(FileEntity.class));
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public List<FileEntity> getFileList(){
        String sql = "select * from file where del_flg=:delFlg";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("delFlg", 1);
        SqlParameterSource ps = new MapSqlParameterSource(map);
        return jdbcTemplate.query(sql, ps, new BeanPropertyRowMapper<FileEntity>(FileEntity.class));
    }

    public boolean deleteFile(Integer fileId){
        String sql = "update file set del_flg=:delFlg where file_id=:fileId";
        Map<String, Object> map = new HashMap<>();
        map.put("delFlg", 0);
        map.put("fileId", fileId);
        SqlParameterSource ps = new MapSqlParameterSource(map);
        int count = jdbcTemplate.update(sql, ps);
        if(count > 0) {
            return true;
        }else{
            return false;
        }
    }

    public FileEntity getFileEntityById(Integer fileId){
        String sql = "select sfu.* from file sfu where sfu.file_id=:fileId";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("fileId", fileId);
        SqlParameterSource ps = new MapSqlParameterSource(map);
        List<FileEntity> list = jdbcTemplate.query(sql, ps, new BeanPropertyRowMapper<FileEntity>(FileEntity.class));
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public FileEntity getFileEntityByFileName(String fileName){
        String sql = "select sfu.* from file sfu where sfu.file_name=:fileName";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("fileName", fileName);
        SqlParameterSource ps = new MapSqlParameterSource(map);
        List<FileEntity> list = jdbcTemplate.query(sql, ps, new BeanPropertyRowMapper<FileEntity>(FileEntity.class));
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public String getLastFileNameByName(String Name, String extName){
        String sql = "select sfu.* from file sfu where sfu.file_name=:fileName";
        Map<String, Object> map = new HashMap<String, Object>();
        List<FileEntity> list;
        int i = 0;
        while (true){
            i++;
            map.put("fileName", Name+"("+i+")"+extName);
            SqlParameterSource ps = new MapSqlParameterSource(map);
            list = jdbcTemplate.query(sql, ps, new BeanPropertyRowMapper<FileEntity>(FileEntity.class));
            if (list == null || list.isEmpty()) {
               if(i == 1){
                   return Name+extName;
               } else {
                   return Name+"("+(i-1)+")"+extName;
               }
            }
            map.clear();
        }
    }

}